package cn.kgc83.shengyin.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;
//import org.springframework.security.access.prepost.PreAuthorize;
import cn.kgc83.shengyin.domain.Survey;
import cn.kgc83.shengyin.domain.Surveyques;
import cn.kgc83.shengyin.service.ISurveyService;
import cn.kgc83.shengyin.util.StrUtil;
import cn.kgc83.shengyin.vo.Page;
import cn.kgc83.shengyin.vo.Result;
import cn.kgc83.shengyin.vo.SurveyQues2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//import com.ruoyi.common.annotation.Log;
//import com.ruoyi.common.core.controller.BaseController;
//import com.ruoyi.common.core.domain.Result;
//import com.ruoyi.common.enums.BusinessType;
//import com.ruoyi.shengyin.domain.Survey;
//import com.ruoyi.shengyin.service.ISurveyService;
//import com.ruoyi.common.utils.poi.ExcelUtil;
//import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 调查问卷Controller
 *
 * @author ruoyi
 * @date 2023-08-01
 */
@RestController
@RequestMapping("/survey")
public class SurveyController extends BaseController {
    @Autowired
    private ISurveyService surveyService;

    /**
     * 查询调查问卷列表
     */
    //@PreAuthorize("@ss.hasPermi('shengyin:survey:list')")
    @GetMapping("/list")
    public Page<Survey> list(Survey survey) {
        startPage();
        List<Survey> list = surveyService.selectSurveyList(survey);
        return getDataTable(list);
    }

    @GetMapping("/generate/{stype}")
    public Result genernate(@PathVariable Long stype) {
        int size = 10;
        List<Long> generate = this.surveyService.generate(stype, size);
        Collections.sort(generate);
        Survey v = new Survey();
        v.setStype(stype);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < generate.size(); i++) {
            sb.append(generate.get(i));
            if (i != generate.size() - 1) {
                sb.append(',');
            }
        }
        v.setChoseques(sb.toString());
        v.setPhone("保密");
        //其他属性 userid...
        return toAjax(this.surveyService.insertSurvey(v));
    }

    /**
     * 预览试卷
     *

     * @return
     */
    @GetMapping("/preview/{id}")
    public Result preview(@PathVariable Long id) {
        Survey survey = this.surveyService.selectSurveyById(id);
        String[] choseques = survey.getChoseques().split(",");
        long[] r = new long[choseques.length];
        for (int i = 0; i < choseques.length; i++) {
            r[i] = StrUtil.toLong(choseques[i], -1);
        }
        List<SurveyQues2> survQueses = this.surveyService.findSurvQueses(r);
        String answer = survey.getChoseanswer();
        if (StrUtil.isBlank(answer) == false) {
            String[] a1 = answer.split(",");
            Map<Long, Integer> m = new HashMap<>();
            for (String c : a1) {
                String[] a2 = c.split(":");
                m.put(StrUtil.toLong(a2[0], -1), StrUtil.toInt(a2[1], -1));
            }
            for (SurveyQues2 q : survQueses) {
                q.setChoice(m.get(q.getId()));
            }
        }
        survey.setQues(survQueses);
        return Result.success(survey);
    }

    @GetMapping("/gotAnswer/{id}")
    public Result gotAnswer(@PathVariable Long id,@RequestBody int[]ids) {

        return Result.success(">>>"+ Arrays.toString(ids));
    }
    /**
     * 导出调查问卷列表
     */
    //@PreAuthorize("@ss.hasPermi('shengyin:survey:export')")
    //@Log(title = "调查问卷", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Survey survey)
//    {
//        List<Survey> list = surveyService.selectSurveyList(survey);
//        ExcelUtil<Survey> util = new ExcelUtil<Survey>(Survey.class);
//        util.exportExcel(response, list, "调查问卷数据");
//    }

    /**
     * 获取调查问卷详细信息
     */
    //@PreAuthorize("@ss.hasPermi('shengyin:survey:query')")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return success(surveyService.selectSurveyById(id));
    }

    /**
     * 新增调查问卷
     */
    //@PreAuthorize("@ss.hasPermi('shengyin:survey:add')")
    //@Log(title = "调查问卷", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody Survey survey) {
        return toAjax(surveyService.insertSurvey(survey));
    }

    /**
     * 修改调查问卷
     */
    //@PreAuthorize("@ss.hasPermi('shengyin:survey:edit')")
    //@Log(title = "调查问卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody Survey survey) {
        return toAjax(surveyService.updateSurvey(survey));
    }

    /**
     * 删除调查问卷
     */
    //@PreAuthorize("@ss.hasPermi('shengyin:survey:remove')")
    //@Log(title = "调查问卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return toAjax(surveyService.deleteSurveyByIds(ids));
    }
}
